﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.DTOs.InstitutionOrder;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    [Route("api/institutionSalesOrders")]
    [ApiController]
    public class InstitutionSalesOrderController : ControllerBase
    {
        private readonly InstitutionSalesOrderService _institutionSalesOrderService;


        public InstitutionSalesOrderController(InstitutionSalesOrderService institutionSalesOrderService)
        {
            _institutionSalesOrderService = institutionSalesOrderService;
        }

        /// <summary>
        /// 得到所有机构订单信息,包含明细
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetAllInstitutionSalesOrders")]
        //[FunctionAuthorize("GetAllInstitutionSalesOrders")]   
        public async Task<ReturnObjectT<IEnumerable<InstitutionSalesOrderDTO>>> GetAllInstitutionSalesOrders(ReceiveObject _receiveObject)
        {
            
            var user = this.User.ToUserModel();
            ReturnObjectT<IEnumerable<InstitutionSalesOrderDTO>> _returnObject = new ReturnObjectT<IEnumerable<InstitutionSalesOrderDTO>>();

            string msg;
            int code;

            var institutionSalesOrders = await _institutionSalesOrderService.GetAllInstitutionSalesOrdersAsync(_receiveObject);

            _returnObject.Code = 1;
            _returnObject.Data = institutionSalesOrders;

            return _returnObject;
            // return Ok(institutionSalesOrders);
        }

        /// <summary>
        /// 得到所有机构订单信息不包含明细
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetAllInstitutionSalesOrderMasters")]
        public async Task<ReturnObjectT<IEnumerable<InstitutionSalesOrderDTO>>> GetAllInstitutionSalesOrderMasters(ReceiveObject _receiveObject)
        {
            ReturnObjectT<IEnumerable<InstitutionSalesOrderDTO>> _returnObject = new ReturnObjectT<IEnumerable<InstitutionSalesOrderDTO>>();

            string msg;
            int code;

            var institutionSalesOrders = await _institutionSalesOrderService.GetAllInstitutionSalesOrderMastersAsync(_receiveObject);

            _returnObject.Code = 1;
            _returnObject.Data = institutionSalesOrders;

            return _returnObject;
            // return Ok(institutionSalesOrders);
        }

        /// <summary>
        /// 根据ID得到 机构订单实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("GetInstitutionSalesOrderById")]
        public async Task<ReturnObjectT<InstitutionSalesOrderDTO>> GetInstitutionSalesOrderById(ReceiveObjectT<int> _receiveObjectT)
        {
            ReturnObjectT<InstitutionSalesOrderDTO> _returnObject = new ReturnObjectT<InstitutionSalesOrderDTO>();

            int id = _receiveObjectT.Data;
            var institutionSalesOrder = await _institutionSalesOrderService.GetInstitutionSalesOrderByIdAsync(id);
            if (institutionSalesOrder == null)
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "获取机构订单信息失败";
            }
            else
            {
                _returnObject.Code = 1;
                _returnObject.Data = (InstitutionSalesOrderDTO)institutionSalesOrder;
            }
            return _returnObject;
          //  return Ok(institutionSalesOrder);
        }

        /// <summary>
        /// 创建机构订单信息
        /// </summary>
        /// <param name="institutionSalesOrder"></param>
        /// <returns></returns>
        [HttpPost("CreateInstitutionSalesOrder")]
        public async Task<IActionResult> CreateInstitutionSalesOrder(InstitutionSalesOrderDTO order)
        {
            ReturnObjectT<InstitutionSalesOrder> _returnObject = new ReturnObjectT<InstitutionSalesOrder>();

            var ret_Order = await _institutionSalesOrderService.CreateInstitutionSalesOrderAsync(order);
            
            return CreatedAtAction(nameof(GetInstitutionSalesOrderById), 
                new { id = ret_Order.Id }, ret_Order);
        }

        /// <summary>
        /// 更新机构订单信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="institutionSalesOrder"></param>
        /// <returns></returns>
        [HttpPost("UpdateInstitutionSalesOrder")]
        public async Task<ReturnObject> UpdateInstitutionSalesOrder(ReceiveObjectT<InstitutionSalesOrder> _receiveObjectT)
        {
            /*
            if (id != institutionSalesOrder.Id)
            {
                return BadRequest();
            }
            */
            ReturnObject _returnObject = new ReturnObject();
            
            InstitutionSalesOrder institutionSalesOrder = _receiveObjectT.Data;
            bool isSuccess = await _institutionSalesOrderService.UpdateInstitutionSalesOrderAsync(institutionSalesOrder);
            if (isSuccess)
            {
                _returnObject.Code = 1;
            }
            else
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "更新机构信息失败";
            }
            return _returnObject;
            // return NoContent();
        }

        /// <summary>
        /// 更新机构订单状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="orderId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost("UpdateInstitutionSalesOrderStatus")]
        public async Task<IActionResult> UpdateInstitutionSalesOrderStatus(UpdateInstitutionOrderStatusRequestDTO req)
        {
            var newOrder = await _institutionSalesOrderService.UpdateInstitutionSalesOrderStatusAsync(req.OrderId, req.Status);
            if (newOrder == null)
                return BadRequest("Cannot find this order");
            return Ok(newOrder);
        }

        /// <summary>
        /// 删除机构订单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("DeleteInstitutionSalesOrder")]
        public async Task<ReturnObject> DeleteInstitutionSalesOrder(ReceiveObjectT<int> _receiveObjectT)
        {
            ReturnObject _returnObject = new ReturnObject();

            
            int id = _receiveObjectT.Data;
            bool isSuccess = await _institutionSalesOrderService.DeleteInstitutionSalesOrderAsync(id);
            if (isSuccess)
            {
                _returnObject.Code = 1;
            }
            else
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "删除机构订单信息失败";
            }
            return _returnObject;
           // return NoContent();
        }
    }
}